package com.onairm.baselibrary.tv;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


/**
 * Created by apple on 2017-5-24.
 */

public class RecyclerGridManagers extends GridLayoutManager {
    private static final String TAG="RecyclerGridManagers";
    private int orientationState=VERTICAL;//默认垂直


    private int itemCount=0;//item总数
    private int findItemIndex=0;//查找item的索引值


    public RecyclerGridManagers(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    public RecyclerGridManagers(Context context, int spanCount) {
        super(context, spanCount);
    }

    public RecyclerGridManagers(Context context, int spanCount, int orientation, boolean reverseLayout) {
        super(context, spanCount, orientation, reverseLayout);
    }

    @Override
    public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state) {
        if(focusSearchFailedListener!=null){
            focusSearchFailedListener.focusSearchFailed(itemCount,findItemIndex,focusDirection,orientationState);
        }
        return super.onFocusSearchFailed(focused, focusDirection, recycler, state);
    }

    // findViewByPosition 失败之后
    private FocusSearchFailedListener focusSearchFailedListener;
    public void setFocusSearchFailedListener(FocusSearchFailedListener listener){
        this.focusSearchFailedListener=listener;
    }
    public interface FocusSearchFailedListener{
        /**
         * @param itemCount item总数
         * @param findItemByIndex 查找item的索引值
         * @param focusDirection 按键方向
         * @param orientationState 布局的方向，垂直或者水平
         */
        void focusSearchFailed(int itemCount, int findItemByIndex, int focusDirection, int orientationState);
    }


    @Override
    public void setOrientation(int orientation) {
        super.setOrientation(orientation);
        orientationState=orientation;
    }



    // 在精选中  每一个item 的最后一个，按右键的相关操作
    private InterceptFocusSearchListener interceptFocusSearchListener;
    public interface  InterceptFocusSearchListener {
        /**
         * @param itemCount  值item总数
         * @param findItemByIndex  查找item的索引值
         */
        void focusSearch(int itemCount, int findItemByIndex);
    }
    public void setInterceptFocusSearchListener(InterceptFocusSearchListener interceptFocusSearchListener){
        this.interceptFocusSearchListener=interceptFocusSearchListener;
    }


    @Override
    public View onInterceptFocusSearch(View focused, int direction) {
      //  return super.onInterceptFocusSearch(focused, direction);
        Log.i(TAG,"onInterceptFocusSearch , focused = "+focused +" direction = "+direction+"    orientationState   "+orientationState);

        if(orientationState==VERTICAL&&(direction==View.FOCUS_UP||direction==View.FOCUS_DOWN)){
         //   LogUtil.d("onInterceptFocusSearch   orientationState=="+orientationState+">>>>direction=="+direction);
            return super.onInterceptFocusSearch(focused, direction);
        }
        if(orientationState==HORIZONTAL&&(direction==View.FOCUS_LEFT||direction==View.FOCUS_RIGHT)){
          //  LogUtil.d("onInterceptFocusSearch   orientationState=="+orientationState+">>>>direction=="+direction);
            return super.onInterceptFocusSearch(focused, direction);
        }

        int span = getSpanCount();
        int count = getItemCount();
        int fromPos = getPosition(focused);
        switch (direction) {
            case View.FOCUS_UP:
                if(orientationState==HORIZONTAL){
                    fromPos=fromPos--;
                }else {
                    fromPos = (fromPos - span);
                }
                break;
            case View.FOCUS_DOWN:
                if(orientationState==HORIZONTAL){
                    fromPos++;
                }else{
                    fromPos = (fromPos + span);
                }
                break;
            case View.FOCUS_RIGHT:
                if(orientationState==HORIZONTAL){
                  //  LogUtil.d("onInterceptFocusSearch   FOCUS_RIGHT=="+orientationState);
                    fromPos=(fromPos+span);
                }else{
                 //   LogUtil.d("onInterceptFocusSearch   FOCUS_RIGHT==else==="+orientationState);
                    fromPos++;
                }
                break;
            case View.FOCUS_LEFT:
                if(orientationState==HORIZONTAL){
                    fromPos=(fromPos-span);
                }else {
                    fromPos--;
                }
                break;
        }

        Log.i(TAG, "onInterceptFocusSearch , fromPos = "+fromPos + " count = " + count+" span = "+span);

        if (interceptFocusSearchListener!=null){
            interceptFocusSearchListener.focusSearch(count,fromPos);
        }

        itemCount=count;
        findItemIndex=fromPos;
        if(fromPos < 0) {
            return focused;
        } else if(fromPos >= count) {
            return focused;
        } else {
            Log.i(TAG, "findviewby>>>>>>>"+fromPos+">>>>>>"+findViewByPosition(fromPos));
            return findViewByPosition(fromPos);
        }
    }





}
